OS=$(uname)
[[ -f /opt/homebrew/bin/brew ]] && eval "$(/opt/homebrew/bin/brew shellenv)"
[ -f ~/.dotfiles_home ] && source ~/.dotfiles_home
[ -f ~/.ssh_auth ] && source ~/.ssh_auth
export PATH=$PATH:/usr/local/bin:~/.local/bin:/opt/bin
shopt -s globstar extglob

include () {
        [[ -f "$1" ]] && source "$1"
}

function reload-bashrc
{
    source ~/.bashrc
}

function reload-profile
{
    source ~/.bash_profile
}

function redo
{
    while :; do $@; done
}

function find_top_dir
{
    if [ "$0" == "-bash" ]; then
       this_dir=`pwd`
    else
       this_dir=$(cd $(dirname $0) && pwd)
       if [ -n "$top_dir" ]; then
            return 0
       fi
    fi

    origin_dir=`pwd`
    target=$1
    echo current:`pwd`, find:$target 

    while [ ! -d $target ]
    do
        #echo `pwd`
        cd ..
        if [ "`pwd`" == "/" ]; then 
            break 
        fi
    done

    if [ `pwd` == / ]; then
        echo "*** Cannot find top_dir which contains:" $target "***"
        cd $this_dir
        #echo "aaaaaa"
        return 1
    else
        top_dir=`pwd`
        export top_dir
        echo "*** top_dir found: $top_dir ***"
    fi
}
#export -f find_top_dir



function add2path
{
    np=$1
    echo "add_to_path:"$np
    gsed  -r 's@(export PATH_DYNADD=)@\1'$np':@'  ~/dotfiles/bash_profile 

}

function check_and_run
{
	if [ -f $1 ]
   	then 
        source $1 
    fi
}

function iconvFile
{
    cf=gbk
    ct=utf8
    for i in $1; do iconv -f $cf -t $ct ${i} > ${i}.bak; mv ${i}.bak ${i}; done
}

#去掉文件每一行最后的0x0d
function trimd
{
    sed  -i "s/\r\$//g" $@
}

function adbs
{
    if [ x$1 == x ]; then
        adb devices | sed '1d'
        return
    fi
    dno=`expr $1 + 1`
    device="-s $(adb devices | sed -n $dno'p' | awk '{print $1}')"
    echo $dno $device
    shift
    adb $device $@
}
export -f adbs

function adbi
{
    if [ x$2 == x ]; then
        adb install -r $1
    else
        adbs $1 install -r $2
    fi
}

function adbt
{
    adb shell su root date $(date +%m%d%H%M%Y.%S)
}

function geny
{
    if [ x$1 == x ]; then
        VBoxManage list vms
        return
    fi
    device=$(VBoxManage list vms | sed -n $1'p' | sed 's/\"\(.*\)\".*/\1/g')
    echo $device
    nohup ~/genymotion/player --vm-name "$device"&
}

function run-sshagent
{
    unset SSH_AUTH_SOCK
    [ -z "$SSH_AUTH_SOCK" ] && eval $(ssh-agent -s)
    ssh-add --apple-use-keychain --apple-load-keychain
    if [ $? -eq 0 ]; then
        if [ ! -z "${TMUX}" ]; then
            tm-setenv SSH_AGENT_PID $SSH_AGENT_PID
            tm-setenv SSH_AUTH_SOCK $SSH_AUTH_SOCK
        fi
        echo "export SSH_AUTH_SOCK=$SSH_AUTH_SOCK" > ~/.ssh_auth
    fi
}


function tmsp
{
    tmux split $1 -c $PWD
}


function set_hkp
{
    export hkp_proxy=$1
}


alias vhkp="vim ~/.hkp_proxy"
function hkp_do
{
    finalv=$hkp_proxy
    if [ -z "$finalv" ]; then 
        if [ -f ~/.hkp_proxy ]; then
            finalv=`cat ~/.hkp_proxy|/usr/bin/sed -n '1p' 2>/dev/null`
        fi
    fi

    no_proxy="192.168.0.0/16,10.96.0.0/16,10.244.0.0/16"

    ipport=${finalv:-http://127.0.0.1:9527}
    echo "hkp_proxy is: $ipport"

    #echo "var:",$exp,$uexp,$p,$d
    if [ ! -z ${exp+x} ]; then
        export http_proxy=$ipport
        export https_proxy=$ipport
        export no_proxy="$no_proxy"
        export HTTP_PROXY=$ipport
        export HTTPS_PROXY=$ipport
        export NO_PROXY="$no_proxy"
        echo 'exported!',$exp
    elif [ ! -z ${uexp+x} ]; then
        unset http_proxy
        unset https_proxy
        unset no_proxy
        echo 'unset!'
    elif [ ! -z ${yarn+x} ]; then
        yarn config set http-proxy http://$ipport 
        yarn config set https-proxy http://$ipport 
    elif [ ! -z ${y+x} ]; then
        #echo 0:$1
        #echo 1:${@:2}
        $1 --http-proxy=$ipport --https-proxy=$ipport ${@:2}
    elif [ ! -z ${p+x} ]; then
        http_proxy=$ipport no_proxy="$no_proxy" https_proxy=$ipport HTTP_PROXY=$ipport HTTPS_PROXY=$ipport $@ --$ipport
    elif [ ! -z ${d+x} ]; then
        http_proxy=$ipport no_proxy="$no_proxy" https_proxy=$ipport $@ -Dhttp.proxyHost=${ipport%:*} -Dhttp.proxyPort=${ipport#*:} -Dhttps.proxyHost=${ipport%:*} -Dhttps.proxyPort=${ipport#*:}
    else
        no_proxy="$no_proxy" http_proxy=$ipport https_proxy=$ipport HTTP_PROXY=$ipport HTTPS_PROXY=$ipport $@ 
    fi
}

function sock5_do
{
    ipport=${hkp_proxy:-127.0.0.1:1080}
    http_proxy=socks5://$ipport https_proxy=socks5://$ipport HTTP_PROXY=socks5://$ipport HTTPS_PROXY=socks5://$ipport $@
}


function exssh {
    ssh $@ -t "export LOCALUSER=`whoami`; bash -l"
}

function ssh-lfs(){
   dir=`readlink -f ${2:-$PWD}`
   rdir=/tmp/sshfs
   echo "ssh and mount:"$dir
   #ssh $1 -R 12000:localhost:22 -t "mkdir -p $rdir; sshfs -p 12000 -o idmap=user,nonempty -o allow_other wellbye@localhost:$dir $rdir; cd $rdir; bash -l"
   ssh $1 -R 12000:localhost:22 -t "mkdir -p ~$rdir; sshfs -p 12000 -o idmap=user wellbye@localhost:$dir ~$rdir; cd ~$rdir; bash -l"
}

#############################################
if [ "$OS" == "Darwin" ] ;then

export PATH=/usr/local/bin:$PATH
[[ -f /opt/homebrew/bin/brew ]] && [[ -r "$(brew --prefix)/etc/profile.d/bash_completion.sh" ]] && . "$(brew --prefix)/etc/profile.d/bash_completion.sh"
. ~/dotfiles/git-completion.bash

alias ctags="`brew --prefix`/bin/ctags"
alias ll='ls -alG'
alias sed=gsed
alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
alias chrome-canary="/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary"
alias mysql="rlwrap -a -- mysql"
alias readlink=greadlink
alias pfr="sudo pfctl -e -f /etc/pf.conf" #重加载防火墙配置
alias unlock-keychain="security unlock-keychain login.keychain"
export LSCOLORS=gxfxbEaEBxxEhEhBaDaCaD

function logs
{
    log stream --info --debug --predicate "sender == \"$1\"" --style syslog
}

settitle() {
    echo -ne "\033]0;$1\007"
}

bs() { brew search $@; }
bi() { brew install $@; }
bci() { brew install --cask $@; }
export  bs
export  bi
export  bci

#############################################
elif [ "$OS" == "Linux" ] ;then

function api
{
    sudo -E apt-get install $@
}
#alias api="sudo apt-get install"
alias aps="apt-cache search"
alias sudo='sudo '
alias ll='ls -al --color=auto'

settitle() {
    echo -ne "\033]0;$1\007"
}

if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

#############################################
elif [[ $OS == CYGWIN* ]]; then
alias ll='ls -al --color=auto'
alias sudo=''
alias open=cygstart
#alias convert="magick convert"
#export CYGWIN="winsymlinks"
export CYGWIN=winsymlinks:native
export JAVA_HOME="/cygdrive/c/Program Files (x86)/Java/jdk1.8.0_31"
export PATH=$PATH:"/cygdrive/c/Program Files (x86)/Java/jdk1.8.0_31/bin"
unset GIT_SSH
function settitle()
{
    echo -ne '\e]0;'$1'\a'
}

#############################################
elif [[ $OS == MINGW* ]]; then
export MSYS=winsymlinks:nativestrict
alias ll='ls -al --color=auto'
alias sudo=''
alias open=cygstart
fi
 
#############################################
#all platform

alias tmuxk='tmux kill-server'
alias tmuxa='tmux attach'
alias cddof='cd ~/dotfiles'
alias bv='echo $BASH_VERSION'
alias kbc='kubectl'
alias kbm='kubeadm'
alias dps="docker ps --format '{{.Image}} / {{.Status}} / {{.Ports}} / {{.Names}}'"
alias dn='dotnet'
alias dns='dotnet-script'
#export PATH=/usr/local/sbin:/usr/local/bin/:$PATH

source $dotfiles_home/bash_util_tmux.sh
source $dotfiles_home/bash_util_docker.sh
source $dotfiles_home/bash_util_git.sh
source $dotfiles_home/bash_util_node.sh
source $dotfiles_home/bash_util_va.sh
#source $dotfiles_home/z.sh



#############################################
#加载本机特定配置文件
[ -f $dotfiles_home/bashrc.`hostname` ] && source $dotfiles_home/bashrc.`hostname`
#############################################

#############################################
#最后加载当前目录下的配置文件
pwd=$(pwd)
[ -f "$pwd/bash.local" ] && . "$pwd/bash.local"
#############################################



[ -f /Users/wellbye/.config/yarn/global/node_modules/@wellbye/jtool/dist/../jtx_bash_completion.sh ] && source /Users/wellbye/.config/yarn/global/node_modules/@wellbye/jtool/dist/../jtx_bash_completion.sh && JTX_BC_TEST=1 
[ -f "$HOME/.cargo/env" ] && . "$HOME/.cargo/env"
